﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace DbView
{
	/*
		Class to provide a context in which to generate a command
		this includes the parent type, the parent view current row,
		the filter string
	*/
	public class ViewContext
	{
		// new improved command. now contains stuff that was in the context
		Command thisCommand = new Command(Commands.Cmnd.Nothing);
		// Preserves previous state across a threading call.
		Command previousCommand = new Command(Commands.Cmnd.Nothing);
		// Viewdef is for table data
		ViewDefinition definition = null;
		// the search entered in the box above the grid.
		String filter = "";
		// Another hacky effort at keeping the table name for the data
		// may now be obsolete if the command object can handle it.
		String objectName = "";

		public Command Command { get { return this.thisCommand; } set {  this.thisCommand = value; } }
		public Command PreviousCommand { get { return this.previousCommand; } set {  this.previousCommand = value; } }
		public ViewDefinition ViewDef { get { return  this.definition; } set {  this.definition = value; } }
		// 
		public String FilterString { get { return this.filter; } set {  this.filter = value; } }
		public String ObjectName { get { return this.objectName;} set {  this.objectName = value; } }
		
		public ViewContext(Command command)
		{
			this.thisCommand = command;
			// look in the command for clues
			// use explicit table (should be object) parameter
			String objectName = command["TABLE"];
			// if not try the captured first column from the grid
			if (objectName.Length == 0)
				objectName = command[Command.TableParameterName(0)];
			// now desparate. This should not ever really be used.
			if (objectName.Length == 0)
				objectName = command.ToString();
			this.objectName = objectName;
		}
	}
}
